package com.gy.ai.agent.util;

import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * @author LGY
 * Create by 2025/6/16 15:13
 */
public class SysUtil {

    public static Throwable getFailureThrowable(Throwable t, Response response) {
        if (t != null) {
            return t;
        }

        if (response != null) {
            String errMessage = "Response code: " + response.code();
            String message = response.message();
            if (StringUtils.isNotEmpty(message)) {
                errMessage += ", message: " + message;
            }
            try (ResponseBody body = response.body()) {
                if (body != null) {
                    String string = body.string();
                    if (StringUtils.isNotEmpty(string)) {
                        errMessage += ", body: " + string;
                    }
                }
            } catch (IOException e) {
                // ignore
            }
            t = new RuntimeException(errMessage);
        }

        return t;

    }
}
